﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StudentManage.Data;

namespace StudentManage.Web.Admin.Management.indexImg
{
    public partial class IndexImgEdit : SecurityPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            if (Request.QueryString["EditID"] != null)
            {
                this.LoadData();
            }
        }

        private void LoadData()
        {
            int imgID =Convert.ToInt32(Request.QueryString["EditID"].ToString());
            ImgList md = ImgList.Where("id=@0", imgID).First();
            this.txt_title.Text = md.Title;
            this.Drop_IsVisible.SelectedValue = Convert.ToBoolean(md.IsVisible) ? "1" : "0";
            this.imgView.Src = "~/Upload/image/" + md.ImgPath;
            this.HidImgPath.Value = md.ImgPath;
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["EditID"] != null)
            {
                int imgID = Convert.ToInt32(Request.QueryString["EditID"].ToString());
                ImgList img = ImgList.Where("id=@0", imgID).First();
                img.Title = this.txt_title.Text.Trim();
                img.ImgPath = this.HidImgPath.Value;
                img.AddTime = DateTime.Now;
                img.IsVisible = (this.Drop_IsVisible.SelectedValue == "1");
                ImgList.Update(img, "id=@0", img.id);
            }
            else
            {
                ImgList img = new ImgList();
                img.Title = this.txt_title.Text.Trim();
                img.ImgPath = this.HidImgPath.Value;
                img.AddTime = DateTime.Now;
                img.IsVisible = (this.Drop_IsVisible.SelectedValue == "1");
                ImgList.Insert(img);
            }
            ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script>alert('保存成功!');window.location.href='IndexImgList.aspx'</script>");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("IndexImgList.aspx");
        }

        protected void Btn_ViewImg_Click(object sender, EventArgs e)
        {
            string strFileName = this.file_ImgPath.FileName;
            string[] strS = strFileName.Split('.');
            string strName = Guid.NewGuid() + "." + strS[strS.Length - 1];
            this.file_ImgPath.SaveAs(MapPath("~/Upload/image/") + strName);
            this.HidImgPath.Value = strName;
            this.imgView.Src = "~/Upload/image/" + strName;
        }
    }
}